package org.fintool.fileio;

import java.io.BufferedWriter;
import java.io.File;
import java.util.ArrayList;

import org.fintool.common.Constants;
import org.fintool.common.PropertyUtil;

public class FileWriter {

	//After writing a file rename the file to a timestamp,in a folder
	static PropertyUtil propertyUtil=null;

	static String strBaseFolderPath=null;

	static{
           propertyUtil=new PropertyUtil();
           if(propertyUtil!=null){
        	   strBaseFolderPath=propertyUtil.getValue(Constants.BASE_FILE_PATH);

           }
	}

public final void createFile(String stock,String exchange) throws Exception{
createFile(stock, exchange, 0);

	}

public final void createTransFormFile(String stock,String exchange) throws Exception{
	createTransFormFile(stock, exchange, 0);

}

   public final void  writeToFileandClose(File file,BufferedWriter buffer,ArrayList contentStrings)throws Exception{


	   for(int i=0;i<contentStrings.size();i++){
		   buffer.write(contentStrings.get(i).toString()+"\r\n");

	   }
	   buffer.close();

   }
   public final void updateFileTS(String stock,String exchange)throws Exception{
	   String strAbsFolderPath=null;
	   boolean isDirSucess=false;

		boolean isFileSuccess=false;

		File directory  = null;
		File[] files=null;

		if(propertyUtil.isDay()){
			strAbsFolderPath=strBaseFolderPath+File.separator+exchange+File.separator+stock+File.separator+Constants.FOLDER_DAY;
			//System.out.println("making directory "+strAbsFolderPath);
			directory=new File(strAbsFolderPath);

			if(directory!=null ){
				files=directory.listFiles();
				if(files!=null){
					for(int i=0;i<files.length;i++){
						files[i].renameTo(new File(strAbsFolderPath+File.separator+System.currentTimeMillis()));

					}


				}

			}




		}

	}

   public final void updateTransformFileTS(String stock,String exchange)throws Exception{
	   String strAbsFolderPath=null;
	   boolean isDirSucess=false;

		boolean isFileSuccess=false;

		File directory  = null;
		File[] files=null;

		if(propertyUtil.isDay()){
			strAbsFolderPath=strBaseFolderPath+File.separator+exchange+File.separator+stock+File.separator+Constants.FOLDER_TRANSFORM;
			//System.out.println("making directory "+strAbsFolderPath);
			directory=new File(strAbsFolderPath);

			if(directory!=null ){
				files=directory.listFiles();
				if(files!=null){
					for(int i=0;i<files.length;i++){
						//files[i].renameTo(new File(strAbsFolderPath+File.separator+System.currentTimeMillis()));
						files[i].renameTo(new File(strAbsFolderPath+File.separator+System.currentTimeMillis()+".fin"));

					}


				}

			}




		}

	}



	public final void createTransFormFile(String stock,String exchange,long timestamp) throws Exception{

		String strAbsFolderPath=null;


		boolean isDirSucess=false;

		boolean isFileSuccess=false;

		File file=null;

		if(propertyUtil.isDay()){
			strAbsFolderPath=strBaseFolderPath+File.separator+exchange+File.separator+stock+File.separator+Constants.FOLDER_TRANSFORM;

			System.out.println("making directory "+strAbsFolderPath);

			isDirSucess=new File(strAbsFolderPath).mkdirs();
			if(isDirSucess){

			}
			else{
				//throw new Exception("Directory Creation Failed");
				System.out.println("Directory  already present");
				deleteDirectoryContents(strAbsFolderPath);
				//file =new File(strAbsFolderPath+File.separator+timestamp);
				file =new File(strAbsFolderPath+File.separator+timestamp+".fin");
				isFileSuccess=file.createNewFile();
				if(isFileSuccess){
					return;

				}else{
					throw new Exception("File Creation Failed");
				}
			}


		}

	}


	public final void createFile(String stock,String exchange,long timestamp) throws Exception{

		String strAbsFolderPath=null;


		boolean isDirSucess=false;

		boolean isFileSuccess=false;

		File file=null;

		if(propertyUtil.isDay()){
			strAbsFolderPath=strBaseFolderPath+File.separator+exchange+File.separator+stock+File.separator+Constants.FOLDER_DAY;
			System.out.println("making directory "+strAbsFolderPath);
			isDirSucess=new File(strAbsFolderPath).mkdirs();
			if(isDirSucess){

			}
			else{
				//throw new Exception("Directory Creation Failed");
				System.out.println("Directory  already present");
				deleteDirectoryContents(strAbsFolderPath);
				file =new File(strAbsFolderPath+File.separator+timestamp);
				isFileSuccess=file.createNewFile();
				if(isFileSuccess){
					return;

				}else{
					throw new Exception("File Creation Failed");
				}
			}


		}

	}
	private void deleteDirectoryContents (final String path ){
		 File directory = new File(path);
		 File file=null;

		    // Make sure the file or directory exists and isn't write protected
		    if (!directory.exists())
		      throw new IllegalArgumentException(
		          "Delete: no such file or directory: " + path);

		    if (!directory.canWrite())
		      throw new IllegalArgumentException("Delete: write protected: "
		          + path);

		    // If it is a directory, make sure it is not empty
		    if (directory.isDirectory()) {
		      String[] files = directory.list();
		      if (files.length > 0){
		    	  for(int i=0;i<1;i++){
		    		  file=new File(path+File.separatorChar+files[i]);
		    		  System.out.println(file.getAbsolutePath());
		    		  if(file.isFile()){
		    			  file.delete();

		    		  }

		    	  }


		      }

		    }

		    // Attempt to delete it



	}

}
